package com.hhsjdesign.test.act;

import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.hhsj.design.pullableview.ability.IPullAble;
import com.hhsj.design.pullableview.scrollview.RefreshScrollViewPro;
import com.hhsjdesign.headerpullrefresh.R;
import com.hhsjtest.scaffold.ScrollScaffoldView;

public class ScrollViewActivity extends AppCompatActivity {

    private Handler handler = new Handler();
    private TextView paramsView;

    RefreshScrollViewPro listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("ScrollView测试");

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        setContentView(R.layout.act_test_scroller);

        ScrollScaffoldView scaffoldView = findViewById(R.id.scaffold);

        listView = (RefreshScrollViewPro) scaffoldView.getScrollView();

        paramsView = findViewById(R.id.params);
        paramsView.setText(getText());

        listView.setRefreshEnable(true);
        listView.setLoadMoreEnable(true);
        listView.setElasticEnable(true);
        listView.setContinuePullEnable(true);

        TextView header = new TextView(this);
        header.setHeight(300);
        header.setBackgroundColor(0xFFFFFFFF);
        header.setText("header1");
        header.setGravity(Gravity.CENTER);


        TextView refresh = new TextView(this);
        refresh.setText("header2");
        refresh.setGravity(Gravity.CENTER);

        listView.addHeaderView(header);
//        scrollView.addHeaderView(refresh);


        listView.addRefreshView(null, 200);
        listView.setLoadMoreView(null, 200);
//        scrollView.addFootLineView(null);


        listView.setRefreshViewMode(RefreshScrollViewPro.REFRESH_VIEW_MODE_TOP);

        listView.setPullAbleListener(new IPullAble.RefreshLoadMoreListener() {
            @Override
            public void doRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        listView.stopRefresh();
                    }
                }, 3_000);
            }

            @Override
            public void doLoadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        recyclerView.showFootLineViewIfCannotLoadMore();
                        listView.stopLoadMore();
                    }
                }, 3_000);
            }
        });


        for (int i = 0; i < 20; i++) {
            TextView textView = new TextView(this);
            textView.setHeight(300);
            textView.setGravity(Gravity.CENTER);
            textView.setBackgroundColor(((i * 16 + i * 16 * 16) << (i % 8 * 3)) | 0x12344568);
            textView.setText(String.format("item %d", (i + 1)));
            scaffoldView.addContentView(textView);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
            case R.id.close_elastic:
                listView.setElasticEnable(false);
                break;

            case R.id.open_elastic:
                listView.setElasticEnable(true);
                break;

            case R.id.close_refresh:
                listView.setRefreshEnable(false);
                break;

            case R.id.open_refresh:
                listView.setRefreshEnable(true);
                break;

            case R.id.elaix_top:
                listView.setRefreshViewMode(IPullAble.REFRESH_VIEW_MODE_TOP);
                break;

            case R.id.elaix_mid:
                listView.setRefreshViewMode(IPullAble.REFRESH_VIEW_MODE_MID);
                break;

            case R.id.close_load_more:
                listView.setLoadMoreEnable(false);
                break;

            case R.id.open_load_more:
                listView.setLoadMoreEnable(true);
                break;

            case R.id.close_footer_line:
                listView.setFooterLineEnable(false);
                break;

            case R.id.open_footer_line:
                listView.setFooterLineEnable(true);
                break;

            case R.id.close_continue_pull:
                listView.setContinuePullEnable(false);
                break;

            case R.id.open_continue_pull:
                listView.setContinuePullEnable(true);
                break;

            case R.id.footerline_hide:
                listView.showFooterLine(false);
                break;

            case R.id.footerline_show:
                listView.showFooterLine(true);
                break;

        }
        paramsView.setText(getText());
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private String getText() {
        StringBuilder builder = new StringBuilder();

        builder.append("弹性功能：").append(listView.isElasticEnable())
                .append("\n").append("弹性位置：").append(listView.getRefreshViewMode())
                .append("\n").append("下拉刷新：").append(listView.isRefreshEnable())
                .append("\n").append("继续下拉：").append(listView.isContinuePullEnable())
                .append("\n").append("上拉加载：").append(listView.isLoadMoreEnable())
                .append("\n").append("底线功能：").append(listView.isFooterLineEnable())
                .append("\n").append("底线展示：").append(listView.isFooterLineShowing())
        ;

        return builder.toString();
    }

}
